package com.sgb.kjwl.view.ui.activity.leaseAsk.publish;

import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import com.sgb.capital.utils.DialogHelper;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAskLeaseHouse2Binding;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.PublishLeashInfoFragment1;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.PublishLeashInfoFragment2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.fragment.PublishLeashInfoFragment3;
import com.sgb.kjwl.viewmodel.machine.PublishLeaseInfoModel2;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.util.MToast;

public class PublishLeaseInfoActivity2 extends AppActivity {

    private PublishLeaseInfoModel2 viewModel;
    private ActivityAskLeaseHouse2Binding binding;
    private String id = "";
    private PublishLeashInfoFragment1 fragment1;
    private PublishLeashInfoFragment2 fragment2;
    private PublishLeashInfoFragment3 fragment3;

    private int type;//0机械,1设备,2车辆,3房产


    public static void start(Context context, int type) {
        Intent intent = new Intent(context, PublishLeaseInfoActivity2.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public static void start(Context context, int type, String id) {
        Intent intent = new Intent(context, PublishLeaseInfoActivity2.class);
        intent.putExtra("type", type);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        viewModel = ViewModelProviders.of(this).get(PublishLeaseInfoModel2.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_ask_lease_house_2);
        binding.setLifecycleOwner(this);
        binding.topLayout.ivBack.setOnClickListener(v -> onBackPressed());
    }

    private void setAppBarTitle() {
        switch (type) {
            case 0:
                binding.topLayout.titleView.setText("机械求租发布");
                break;
            case 1:
                binding.topLayout.titleView.setText("设备求租发布");
                break;
            case 2:
                binding.topLayout.titleView.setText("车辆求租发布");
                break;
            case 3:
                binding.topLayout.titleView.setText("房产求租发布");
                break;
            default:
                break;
        }
    }

    private void recevierliveData() {
        viewModel.selectFragLiveData.observe(this, data -> replaceFragment(data));
        viewModel.showLoad.observe(this, aBoolean -> {
            if (aBoolean) {
                DialogHelper.showProgressDialog(getActivity(), "加载中...", 0, false).setCanceledOnTouchOutside(false);
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        viewModel.showLoad2.observe(this, aBoolean -> {
            if (aBoolean) {
                DialogHelper.showProgressDialog(getActivity(), "提交中", 0, false).setCanceledOnTouchOutside(false);
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        viewModel.msgLiveData.observe(this, data -> MToast.showToast(getActivity(), data));
        viewModel.isSuccessDataLiveData.observe(this, data -> {
            int category = type + 1;
            PublishSuccessActivity.start(getActivity(), data, category + "");
            finish();
        });
    }

    @Override
    protected void init() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置沉浸式状态栏的上移问题
        setTileImmerse(true);
        type = getIntent().getIntExtra("type", 0);
        id = getIntent().getStringExtra("id");
        setAppBarTitle();
        recevierliveData();
        replaceFragment(0);
    }

    private void replaceFragment(int flag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (flag == 0) {
            if (fragment1 == null) {
                fragment1 = PublishLeashInfoFragment1.newInstance(id, type);
                transaction.add(binding.frameLayout.getId(), fragment1);
            }
            hideAllFragment();
            transaction.show(fragment1);
        }
        if (flag == 1) {
            if (fragment2 == null) {
                fragment2 = PublishLeashInfoFragment2.newInstance(id, type);
                transaction.add(binding.frameLayout.getId(), fragment2);
            }
            hideAllFragment();
            transaction.show(fragment2);
        }
        if (flag == 2) {
            if (fragment3 == null) {
                fragment3 = PublishLeashInfoFragment3.newInstance(id, type);
                transaction.add(binding.frameLayout.getId(), fragment3);
            }
            hideAllFragment();
            transaction.show(fragment3);
        }
        transaction.commit();
    }

    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
            initDialog();
            return;
    }

    public void initDialog() {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("提示")
                .setMsg("确定要退出吗？已填写的内容将不会保存！")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        PublishLeaseInfoActivity2.this.finish();
                    }

                    @Override
                    public void onLeftClick() {
                        return;
                    }
                }).show();
    }
}
